package com.rasenhund.android.grecorder.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;
import com.rasenhund.android.grecorder.schema.StageRecord;

/**
 * カテゴリ毎の集計結果表示画面
 */
public class ResultCategoryActivity extends BaseActivity {
    @SuppressWarnings("unused")
    private static final String TAG = ResultCategoryActivity.class.getSimpleName();
    private final ResultCategoryActivity self = this;

    private int mStageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_stage_category);

        // ヘッダ
        mTxtArmyEfsf = (TextView) findViewById(R.id.txtArmy_Efsf);
        mTxtArmyPoz = (TextView) findViewById(R.id.txtArmy_Poz);

        // パラメータ取得
        getParam();

        // 広告エリアの初期化
        initAdView();

        // Viewの初期化
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();

        // 軍属に応じてヘッダを変更
        setArmyHeaderVisibility(mArmyCd);

        // 表示用のデータ取得
        getStageResult(mStageId);

    }

    /**
     * パラメータの取得
     */
    void getParam() {

        Intent intent = getIntent();

        if (intent != null) {
            mStageId = intent.getIntExtra(Const.KEY.KEY_STAGE, Const.STAGE.ALL);
        }

        if (mStageId == -1) {
            finishActivity(RESULT_CANCELED);
        }

    }

    /**
     * Viewの初期化
     */
    private void initView() {

        View view;
        TextView textView;

        view = (RelativeLayout) findViewById(R.id.lstGrapp);
        textView = (TextView) view.findViewById(R.id.txtStageNm);
        textView.setText(R.string.category_grapp);

        view = (RelativeLayout) findViewById(R.id.lstProx);
        textView = (TextView) view.findViewById(R.id.txtStageNm);
        textView.setText(R.string.category_prox);

        view = (RelativeLayout) findViewById(R.id.lstShoot);
        textView = (TextView) view.findViewById(R.id.txtStageNm);
        textView.setText(R.string.category_shoot);

        view = (RelativeLayout) findViewById(R.id.lstCannon);
        textView = (TextView) view.findViewById(R.id.txtStageNm);
        textView.setText(R.string.category_cannon);

        view = (RelativeLayout) findViewById(R.id.lstSnipe);
        textView = (TextView) view.findViewById(R.id.txtStageNm);
        textView.setText(R.string.category_snip);

    }

    /**
     * 戦績の取得
     * 
     * @param stage
     */
    private void getStageResult(int stageId) {

        ArrayList<StageRecord> list = null;

        DbHelper dbHelper = null;
        SQLiteDatabase db = null;

        try {

            dbHelper = new DbHelper(self);
            db = dbHelper.getWritableDatabase();
            RecordDao dao = new RecordDao(db);

            list = dao.getResultByCategory(mArmyCd, stageId);

        } finally {
            if (db.isOpen()) {
                db.close();
            }
        }

        View view = null;
        StageRecord stage = null;

        for (int i = 0; i < list.size(); i++) {
            stage = list.get(i);
            stage.calcPerWin();

            switch (stage.getCategoryCd()) {
                case Const.CATEGORY.GRAPP:
                    view = (RelativeLayout) findViewById(R.id.lstGrapp);
                    break;
                case Const.CATEGORY.PROX:
                    view = (RelativeLayout) findViewById(R.id.lstProx);
                    break;
                case Const.CATEGORY.SHOOT:
                    view = (RelativeLayout) findViewById(R.id.lstShoot);
                    break;
                case Const.CATEGORY.CANNON:
                    view = (RelativeLayout) findViewById(R.id.lstCannon);
                    break;
                case Const.CATEGORY.SNIPE:
                    view = (RelativeLayout) findViewById(R.id.lstSnipe);
                    break;
                default:
                    break;
            }

            ((TextView) view.findViewById(R.id.txtTotal)).setText(
                    String.valueOf(stage.getTotalCount()));
            ((TextView) view.findViewById(R.id.txtWin)).setText(
                    String.valueOf(stage.getWinCount()));
            ((TextView) view.findViewById(R.id.txtLose)).setText(
                    String.valueOf(stage.getLoseCount()));
            ((TextView) view.findViewById(R.id.txtDraw)).setText(
                    String.valueOf(stage.getDrawCount()));
            ((TextView) view.findViewById(R.id.txtPer)).setText(
                    String.valueOf(stage.getPerWin()));

        }

    }

}
